/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

import java.awt.HeadlessException;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 *
 * @author gean
 */
public class Utilisateurs extends javax.swing.JFrame {

   private final Connection conn;
    private ResultSet rs=null;
    
    public Utilisateurs() {
        initComponents();
        conn =gestion_chambes_hotel.Gestion_Chambes_Hotel.DB_Connection();
        supp.setVisible(false);
        ajo.setVisible(false);
        mod.setVisible(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ajo = new javax.swing.JPanel();
        que3 = new javax.swing.JTextField();
        rep3 = new javax.swing.JTextField();
        pass3 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        user3 = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel26 = new javax.swing.JLabel();
        supp = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        user1 = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel27 = new javax.swing.JLabel();
        mod = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        user2 = new javax.swing.JTextField();
        pass1 = new javax.swing.JTextField();
        que1 = new javax.swing.JTextField();
        rep1 = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        jLabel28 = new javax.swing.JLabel();
        Enre = new javax.swing.JLabel();
        sup = new javax.swing.JLabel();
        jLabel24 = new javax.swing.JLabel();
        jLabel25 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setBackground(new java.awt.Color(153, 153, 153));
        setMinimumSize(new java.awt.Dimension(787, 489));
        setPreferredSize(new java.awt.Dimension(787, 489));
        getContentPane().setLayout(null);

        ajo.setBackground(new java.awt.Color(204, 204, 204));
        ajo.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        ajo.setLayout(null);

        que3.setBorder(null);
        ajo.add(que3);
        que3.setBounds(120, 140, 160, 30);

        rep3.setBorder(null);
        ajo.add(rep3);
        rep3.setBounds(120, 200, 170, 30);

        pass3.setBorder(null);
        ajo.add(pass3);
        pass3.setBounds(120, 90, 170, 30);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Username :");
        ajo.add(jLabel1);
        jLabel1.setBounds(0, 20, 90, 30);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Mot de passe :");
        ajo.add(jLabel2);
        jLabel2.setBounds(0, 90, 110, 30);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setText("Question :");
        ajo.add(jLabel3);
        jLabel3.setBounds(12, 139, 90, 30);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel4.setText("Reponse :");
        ajo.add(jLabel4);
        jLabel4.setBounds(10, 200, 80, 30);

        user3.setBorder(null);
        ajo.add(user3);
        user3.setBounds(120, 20, 170, 30);

        jLabel12.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Buttons/Ajo_Util.png"))); // NOI18N
        jLabel12.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jLabel12MouseReleased(evt);
            }
        });
        ajo.add(jLabel12);
        jLabel12.setBounds(160, 250, 120, 40);

        jLabel13.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/txt1.png"))); // NOI18N
        ajo.add(jLabel13);
        jLabel13.setBounds(100, 10, 220, 50);

        jLabel14.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/txt1.png"))); // NOI18N
        ajo.add(jLabel14);
        jLabel14.setBounds(104, 80, 220, 50);

        jLabel15.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/txt1.png"))); // NOI18N
        ajo.add(jLabel15);
        jLabel15.setBounds(100, 136, 230, 40);

        jLabel16.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/txt1.png"))); // NOI18N
        ajo.add(jLabel16);
        jLabel16.setBounds(100, 190, 220, 50);

        jLabel26.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Accueil/wallhaven-19788.jpg"))); // NOI18N
        ajo.add(jLabel26);
        jLabel26.setBounds(0, 0, 340, 320);

        getContentPane().add(ajo);
        ajo.setBounds(430, 80, 320, 310);

        supp.setBackground(new java.awt.Color(153, 153, 153));
        supp.setLayout(null);

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("Username :");
        supp.add(jLabel5);
        jLabel5.setBounds(10, 40, 90, 23);

        user1.setBorder(null);
        user1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                user1MouseClicked(evt);
            }
        });
        supp.add(user1);
        user1.setBounds(113, 37, 176, 30);

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Buttons/Supp_Util.png"))); // NOI18N
        jLabel6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jLabel6MouseReleased(evt);
            }
        });
        supp.add(jLabel6);
        jLabel6.setBounds(202, 86, 130, 30);

        jLabel17.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/txt1.png"))); // NOI18N
        supp.add(jLabel17);
        jLabel17.setBounds(100, 20, 230, 60);

        jLabel27.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Accueil/wallhaven-19788.jpg"))); // NOI18N
        supp.add(jLabel27);
        jLabel27.setBounds(0, 0, 440, 210);

        getContentPane().add(supp);
        supp.setBounds(40, 50, 360, 130);

        mod.setBackground(new java.awt.Color(204, 204, 204));
        mod.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        mod.setLayout(null);

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel7.setText("Username :");
        mod.add(jLabel7);
        jLabel7.setBounds(20, 0, 100, 30);

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel8.setText("Mot de passe :");
        mod.add(jLabel8);
        jLabel8.setBounds(0, 60, 120, 30);

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel9.setText("Question :");
        mod.add(jLabel9);
        jLabel9.setBounds(28, 110, 80, 30);

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel10.setText("Reponse :");
        mod.add(jLabel10);
        jLabel10.setBounds(30, 160, 71, 30);

        user2.setBorder(null);
        user2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                user2MouseClicked(evt);
            }
        });
        user2.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                user2KeyPressed(evt);
            }
        });
        mod.add(user2);
        user2.setBounds(140, 10, 144, 30);

        pass1.setBorder(null);
        mod.add(pass1);
        pass1.setBounds(140, 60, 144, 30);

        que1.setBorder(null);
        mod.add(que1);
        que1.setBounds(140, 112, 144, 30);

        rep1.setBorder(null);
        mod.add(rep1);
        rep1.setBounds(140, 162, 144, 30);

        jLabel11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Buttons/Mod_Util.png"))); // NOI18N
        jLabel11.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jLabel11MouseReleased(evt);
            }
        });
        mod.add(jLabel11);
        jLabel11.setBounds(170, 227, 130, 40);

        jLabel18.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/txt1.png"))); // NOI18N
        mod.add(jLabel18);
        jLabel18.setBounds(120, 0, 230, 50);

        jLabel19.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/txt1.png"))); // NOI18N
        mod.add(jLabel19);
        jLabel19.setBounds(120, 50, 214, 50);

        jLabel20.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/txt1.png"))); // NOI18N
        mod.add(jLabel20);
        jLabel20.setBounds(120, 110, 214, 36);

        jLabel21.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Labels/txt1.png"))); // NOI18N
        mod.add(jLabel21);
        jLabel21.setBounds(120, 160, 214, 36);

        jLabel28.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Accueil/wallhaven-19788.jpg"))); // NOI18N
        mod.add(jLabel28);
        jLabel28.setBounds(-260, -30, 650, 350);

        getContentPane().add(mod);
        mod.setBounds(20, 190, 380, 290);

        Enre.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Buttons/Enregistrer_Client.png"))); // NOI18N
        Enre.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                EnreMouseReleased(evt);
            }
        });
        getContentPane().add(Enre);
        Enre.setBounds(240, 10, 150, 40);

        sup.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Buttons/Delete.png"))); // NOI18N
        sup.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                supMouseReleased(evt);
            }
        });
        getContentPane().add(sup);
        sup.setBounds(400, 10, 140, 40);

        jLabel24.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Buttons/modify.png"))); // NOI18N
        jLabel24.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jLabel24MouseReleased(evt);
            }
        });
        getContentPane().add(jLabel24);
        jLabel24.setBounds(570, 10, 150, 40);

        jLabel25.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Main/Images/Accueil/wallhaven-19788.jpg"))); // NOI18N
        getContentPane().add(jLabel25);
        jLabel25.setBounds(0, 0, 800, 510);

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents
    
    private void selection()
    {
        String user=user2.getText();
        try {
            
            String sql0="select * from utilisateur where LOGIN='"+user+"'";
            PreparedStatement ps1=conn.prepareStatement(sql0);
            rs=ps1.executeQuery();
            if(rs.next()){
            
                String pass=rs.getString("MOT_PASSE");
                pass1.setText(pass);
                
                String que=rs.getString("QUESTION");
                que1.setText(que);
                
                String rep=rs.getString("REPONSE");
                rep1.setText(rep);
            
            }
            else{
                JOptionPane.showMessageDialog(null, "Utilisateur n'existe pas");
            }
        } catch (Exception e) {
        }
    
    }
    public void Modifier(){
    
        sup.setVisible(false);
        Enre.setVisible(false);
        
    
    }
    
    private void jLabel6MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel6MouseReleased
        
        String user=user1.getText();
        try {
                
                String sql0="select * from utilisateur where LOGIN='"+user+"'";
                String sql_sup="delete from utilisateur where LOGIN='"+user+"'";
                
                 PreparedStatement ps1=conn.prepareStatement(sql0);
                 PreparedStatement ps2=conn.prepareStatement(sql_sup);
                 
                 rs=ps1.executeQuery();
                 if(rs.next())
                    {
                        
                        if(JOptionPane.showConfirmDialog(null, "Supprimer","Vous etes sur?",JOptionPane.YES_NO_CANCEL_OPTION)==JOptionPane.YES_OPTION){
                            
                            ps2.executeUpdate();
                            JOptionPane.showMessageDialog(null, "l'utilisateur Supprimer!");
                        }
                        
                    }
                 else{
                 JOptionPane.showMessageDialog(null, "Ce utilisateur n'existe pas!");
                 }
        } catch (SQLException | HeadlessException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        
        
    }//GEN-LAST:event_jLabel6MouseReleased

    private void user1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_user1MouseClicked
        user1.setText("");
    }//GEN-LAST:event_user1MouseClicked

    private void user2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_user2MouseClicked
        user2.setText("");
    }//GEN-LAST:event_user2MouseClicked

    private void user2KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_user2KeyPressed
        if(evt.getKeyCode()==KeyEvent.VK_ENTER){
            selection();
        }
        
    }//GEN-LAST:event_user2KeyPressed

    private void jLabel11MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel11MouseReleased
        String user=user1.getText();
        try {
            
            String sql_insert="update utilisateur set MOT_PASSE=?,QUESTION=?,REPONSE=? where LOGIN=?";
            PreparedStatement ps1 =conn.prepareStatement(sql_insert);
            
            
            ps1.setString(1, pass1.getText());
            ps1.setString(2, que1.getText());
            ps1.setString(3, rep1.getText());
            ps1.setString(4, user2.getText());
            
            if(JOptionPane.showConfirmDialog(null, "Mettre a Jour?","Vous etes sur?",JOptionPane.YES_NO_CANCEL_OPTION)==JOptionPane.YES_OPTION){
                            
                            ps1.executeUpdate();
                            JOptionPane.showMessageDialog(null, "l'utilisateur  modifier!");
                            
                            
                            Login l = new Login();
                            l.setVisible(true);
                            setVisible(false);
                        }
            
            
        } catch (SQLException | HeadlessException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        
        
    }//GEN-LAST:event_jLabel11MouseReleased

    private void jLabel12MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel12MouseReleased
       
        
        try {
            
            String sql_insert="insert into utilisateur (LOGIN,MOT_PASSE,QUESTION,REPONSE) values(?,?,?,?)";
            PreparedStatement ps1 =conn.prepareStatement(sql_insert);
            
            
            ps1.setString(1, user3.getText());
            ps1.setString(2, pass3.getText());
            ps1.setString(3, que3.getText());
            ps1.setString(4, rep3.getText());
            
            
            if(JOptionPane.showConfirmDialog(null, "Enregistrer?","Vous etes sur?",JOptionPane.YES_NO_CANCEL_OPTION)==JOptionPane.YES_OPTION){
                            
                            ps1.executeUpdate();
                            JOptionPane.showMessageDialog(null, "l'utilisateur  Ajouté!");
                        }
            
            
        } catch (SQLException | HeadlessException e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
        
        
        
    }//GEN-LAST:event_jLabel12MouseReleased

    private void EnreMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_EnreMouseReleased
        ajo.setVisible(true);
        supp.setVisible(false);
        mod.setVisible(false);
    }//GEN-LAST:event_EnreMouseReleased

    private void supMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_supMouseReleased
        supp.setVisible(true);
        mod.setVisible(false);
        ajo.setVisible(false);
    }//GEN-LAST:event_supMouseReleased

    private void jLabel24MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel24MouseReleased
        mod.setVisible(true);
        ajo.setVisible(false);
        supp.setVisible(false);
    }//GEN-LAST:event_jLabel24MouseReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Utilisateurs.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Utilisateurs.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Utilisateurs.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Utilisateurs.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Utilisateurs().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Enre;
    private javax.swing.JPanel ajo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel24;
    private javax.swing.JLabel jLabel25;
    private javax.swing.JLabel jLabel26;
    private javax.swing.JLabel jLabel27;
    private javax.swing.JLabel jLabel28;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel mod;
    private javax.swing.JTextField pass1;
    private javax.swing.JTextField pass3;
    private javax.swing.JTextField que1;
    private javax.swing.JTextField que3;
    private javax.swing.JTextField rep1;
    private javax.swing.JTextField rep3;
    private javax.swing.JLabel sup;
    private javax.swing.JPanel supp;
    private javax.swing.JTextField user1;
    private javax.swing.JTextField user2;
    private javax.swing.JTextField user3;
    // End of variables declaration//GEN-END:variables
}
